//*****************************************************************************
//
//								Monitor_DDR.c
//
//*****************************************************************************
//
//
#include "Config.h"
#include "reg.h"
#include "reg_debug.h"
#include "typedefs.h"
#include "printf.h"
#include "CPU.h"
#include "i2c.h"
#include "util.h"
#include "monitor_SPI.h"
#include "SPI.h"
#include "Global.h"

//=============================================================================
#define OSD_LUT_WINNO			0x7a
#define OSD_LUT_INDEX			0x7b
#define OSD_LUT_B				0x7c
#define OSD_LUT_G				0x7d
#define OSD_LUT_R				0x7e
#define OSD_LUT_ATTR			0x7f		

//=============================================================================
//
//=============================================================================
void MonDDR(void)
{
	WORD i, j;
	static DWORD addr = 0L;

	//---------------------- Dump DDR --------------------------
	if( !stricmp( argv[0], "dd" ) ) {
		WORD cnt = 0x100;

		if( argc>=2 ) addr = a2h( argv[1] );
		if( argc>=3 ) cnt = a2h( argv[2] );

		Printf("\nDump DDR %06lx", addr);

		WriteTW88(0xff, 0x0c/*MCU_PAGE*/);
		WriteTW88(0xf2, addr>>16);		// addr
		WriteTW88(0xf3, addr>>8);		//
		WriteTW88(0xf4, addr);			//
		WriteTW88(0xf5, cnt>>8);		// len
		WriteTW88(0xf6, cnt);			//
		WriteTW88(0xf7, 0x02);			// Read Req.

		for(j=0; j<cnt/0x20; j++) {
			Printf("\n%06lx:", j*0x10L + addr);
			for(i=0; i<0x10; i++) Printf(" %02bx", ReadTW88(0xf0));
			Printf("-");
			for(i=0; i<0x10; i++) Printf("%02bx ", ReadTW88(0xf0));
		}
		WriteTW88(0xf7, 0x00);			// Read Req=0, Write Req=0.

		addr += cnt/2;


	}
	//---------------------- Write DDR ------------------------
	else if( !stricmp( argv[0], "dramp" ) ) {

		WORD cnt = 0x400;

		WriteTW88( OSD_LUT_WINNO, 0 );

		for(i=0; i<256; i++) {
			WriteTW88( OSD_LUT_INDEX, i );
			WriteTW88( OSD_LUT_R, i );
			WriteTW88( OSD_LUT_G, i );
			WriteTW88( OSD_LUT_B, i );
			WriteTW88( OSD_LUT_ATTR, 0 );
		}

		if( argc>=2 ) addr = a2h( argv[1] );
		if( argc>=3 ) cnt = a2h( argv[2] );

		Printf("\nWrite ramp to DDR %06lx, %04x", addr, cnt);
		
		WriteTW88(0xff, 0x0c/*MCU_PAGE*/);
		WriteTW88(0xf2, addr>>16);		// addr
		WriteTW88(0xf3, addr>>8);		//
		WriteTW88(0xf4, addr);			//
		WriteTW88(0xf5, cnt>>8);		// len
		WriteTW88(0xf6, cnt);			//
		WriteTW88(0xf7, 0x01);			// Write Req.

		for(i=0; i<cnt; i++) {
			WriteTW88(0xf0, i);
			if( ((i+1) % 64)==0 ) delay(1);
		}
		delay(1);
		WriteTW88(0xf7, 0x00);			// Read Req=0, Write Req=0.

		addr += cnt/2;

	}

	//--------------------------------------------------------
	else
		Printf("\nInvalid command...");	
	
}






